<template>
  <component
    is="a-modal"
    v-model:visible="visible"
    :width="tool.getDevice() === 'mobile' ? '100%' : '600px'"
    :title="title"
    :mask-closable="false"
    :ok-loading="loading"
    @cancel="close"
    @before-ok="submit">
    <!-- 表单信息 start -->
    <a-form ref="formRef" :model="formData" :rules="rules" :auto-label-width="true">
      <a-form-item label="团长姓名" field="name">
        <a-input v-model="formData.name" placeholder="请输入团长姓名" />
      </a-form-item>
      <a-form-item label="手机号" field="phone">
        <a-input v-model="formData.phone" placeholder="请输入手机号" />
      </a-form-item>
      <a-form-item label="社区名称" field="community_name">
        <a-input v-model="formData.community_name" placeholder="请输入社区名称" />
      </a-form-item>
      <a-form-item label="省/市/区县" field="province">
        <ma-cityLinkage v-model="formData.address_info" type="cascader" @change="changeAddressInfo" />
      </a-form-item>
      <a-form-item label="详细地址" field="address">
        <a-input v-model="formData.address" placeholder="请输入详细地址" />
      </a-form-item>
      <a-form-item label="门头照片" field="image">
        <sa-upload-image v-model="formData.image" :rounded="false" />
      </a-form-item>
    </a-form>
    <!-- 表单信息 end -->
  </component>
</template>

<script setup>
import { ref, reactive, computed } from 'vue'
import { Message } from '@arco-design/web-vue'
import tool from '@/utils/tool'
import api from '@/api/leader'

const emit = defineEmits(['success'])

// 引用定义
const formRef = ref()
const mode = ref('')
const visible = ref(false)
const loading = ref(false)

let title = computed(() => {
  return '团长管理' + (mode.value == 'add' ? '-新增' : '-编辑')
})

// 表单初始值
const initialFormData = {
  id: '',
  name: '',
  phone: '',
  community_name: '',
  province: '',
  city: '',
  area: '',
  address: '',
  image: '',
  address_info:''
}

// 表单信息
const formData = reactive({ ...initialFormData })

// 验证规则
const rules = {
  name: [{ required: true, message: '团长姓名不能为空' }],
  phone: [{ required: true, message: '手机号不能为空' }],
  community_name: [{ required: true, message: '社区名称不能为空' }],
  province: [{ required: true, message: '省份不能为空' }],
  city: [{ required: true, message: '城市不能为空' }],
  district: [{ required: true, message: '区县不能为空' }],
  address: [{ required: true, message: '详细地址不能为空' }],
}

// 打开弹框
const open = async (type = 'add') => {
  mode.value = type
  // 重置表单数据
  Object.assign(formData, initialFormData)
  // 初始化地址信息
  formRef.value.clearValidate()
  visible.value = true
  await initPage()
}

// 初始化页面数据
const initPage = async () => {}

// 设置数据
const setFormData = async (data) => {
  for (const key in formData) {
    if (data[key] != null && data[key] != undefined) {
      formData[key] = data[key]
    }
  }
  // 初始化地址信息
  formData.address_info = formData.province + ' / ' + formData.city + ' / ' + formData.area
}

const changeAddressInfo = (val) => {
  formData.province = val[0]
  formData.city = val[1]
  formData.area = val[2]
  console.log('changeAddressInfo', formData)
}

// 数据保存
const submit = async (done) => {
  const validate = await formRef.value?.validate()
  if (!validate) {
    loading.value = true
    let data = { ...formData }
    let result = {}
    if (mode.value === 'add') {
      // 添加数据
      data.id = undefined
      result = await api.save(data)
    } else {
      // 修改数据
      result = await api.update(data)
    }
    if (result.code === 200) {
      Message.success('操作成功')
      emit('success')
      done(true)
    }
    // 防止连续点击提交
    setTimeout(() => {
      loading.value = false
    }, 500)
  }
  done(false)
}

// 关闭弹窗
const close = () => (visible.value = false)

defineExpose({ open, setFormData })
</script>
